<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width, initial-scale=1.0,viewport-fit=cover"><title>兴趣课-时钟的实现 | Ash's blog</title><meta name="author" content="Ash"><meta name="copyright" content="Ash"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="一、关于Canvas1. 什么是canvas1&lt;canvas&gt;是一个可以使用脚本(通常为JavaScript)在其中绘制图形的 HTML 元素。 2. 基础结构12345678&lt;canvas id&#x3D;&quot;mycanvas&quot; width&#x3D;&quot;200&quot; height&#x3D;&quot;300&quot;&gt;   您的浏览器不支持canvas&lt;&#x2F;c">
<meta property="og:type" content="article">
<meta property="og:title" content="兴趣课-时钟的实现">
<meta property="og:url" content="http://www.ashblog.top/2024/05/21/%E5%85%B4%E8%B6%A3%E8%AF%BE-%E6%97%B6%E9%92%9F%E7%9A%84%E5%AE%9E%E7%8E%B0/index.html">
<meta property="og:site_name" content="Ash&#39;s blog">
<meta property="og:description" content="一、关于Canvas1. 什么是canvas1&lt;canvas&gt;是一个可以使用脚本(通常为JavaScript)在其中绘制图形的 HTML 元素。 2. 基础结构12345678&lt;canvas id&#x3D;&quot;mycanvas&quot; width&#x3D;&quot;200&quot; height&#x3D;&quot;300&quot;&gt;   您的浏览器不支持canvas&lt;&#x2F;c">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm12.webp">
<meta property="article:published_time" content="2024-05-21T01:52:57.000Z">
<meta property="article:modified_time" content="2024-05-21T02:19:16.979Z">
<meta property="article:author" content="Ash">
<meta property="article:tag" content="HTML">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm12.webp"><link rel="shortcut icon" href="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/A%20(1).svg"><link rel="canonical" href="http://www.ashblog.top/2024/05/21/%E5%85%B4%E8%B6%A3%E8%AF%BE-%E6%97%B6%E9%92%9F%E7%9A%84%E5%AE%9E%E7%8E%B0/index.html"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><link rel="stylesheet" href="/css/index.css?v=4.13.0"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@6.5.1/css/all.min.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = {
  root: '/',
  algolia: undefined,
  localSearch: {"path":"/search.xml","preload":"ture","top_n_per_article":1,"unescape":false,"languages":{"hits_empty":"找不到您查询的内容：${query}","hits_stats":"共找到 ${hits} 篇文章"}},
  translate: undefined,
  noticeOutdate: undefined,
  highlight: {"plugin":"highlight.js","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  dateSuffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  infinitegrid: {
    js: 'https://cdn.jsdelivr.net/npm/@egjs/infinitegrid@4.11.1/dist/infinitegrid.min.js',
    buttonText: '加载更多'
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isAnchor: false,
  percent: {
    toc: true,
    rightside: false,
  },
  autoDarkmode: true
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: '兴趣课-时钟的实现',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2024-05-21 10:19:16'
}</script><script>(win=>{
      win.saveToLocal = {
        set: (key, value, ttl) => {
          if (ttl === 0) return
          const now = Date.now()
          const expiry = now + ttl * 86400000
          const item = {
            value,
            expiry
          }
          localStorage.setItem(key, JSON.stringify(item))
        },
      
        get: key => {
          const itemStr = localStorage.getItem(key)
      
          if (!itemStr) {
            return undefined
          }
          const item = JSON.parse(itemStr)
          const now = Date.now()
      
          if (now > item.expiry) {
            localStorage.removeItem(key)
            return undefined
          }
          return item.value
        }
      }
    
      win.getScript = (url, attr = {}) => new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.src = url
        script.async = true
        script.onerror = reject
        script.onload = script.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          script.onload = script.onreadystatechange = null
          resolve()
        }

        Object.keys(attr).forEach(key => {
          script.setAttribute(key, attr[key])
        })

        document.head.appendChild(script)
      })
    
      win.getCSS = (url, id = false) => new Promise((resolve, reject) => {
        const link = document.createElement('link')
        link.rel = 'stylesheet'
        link.href = url
        if (id) link.id = id
        link.onerror = reject
        link.onload = link.onreadystatechange = function() {
          const loadState = this.readyState
          if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
          link.onload = link.onreadystatechange = null
          resolve()
        }
        document.head.appendChild(link)
      })
    
      win.activateDarkMode = () => {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = () => {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          const isDarkMode = window.matchMedia('(prefers-color-scheme: dark)').matches
          const isLightMode = window.matchMedia('(prefers-color-scheme: light)').matches
          const isNotSpecified = window.matchMedia('(prefers-color-scheme: no-preference)').matches
          const hasNoSupport = !isDarkMode && !isLightMode && !isNotSpecified

          if (t === undefined) {
            if (isLightMode) activateLightMode()
            else if (isDarkMode) activateDarkMode()
            else if (isNotSpecified || hasNoSupport) {
              const now = new Date()
              const hour = now.getHours()
              const isNight = hour <= 8 || hour >= 22
              isNight ? activateDarkMode() : activateLightMode()
            }
            window.matchMedia('(prefers-color-scheme: dark)').addListener(e => {
              if (saveToLocal.get('theme') === undefined) {
                e.matches ? activateDarkMode() : activateLightMode()
              }
            })
          } else if (t === 'light') activateLightMode()
          else activateDarkMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
      const detectApple = () => {
        if(/iPad|iPhone|iPod|Macintosh/.test(navigator.userAgent)){
          document.documentElement.classList.add('apple')
        }
      }
      detectApple()
    })(window)</script><link rel="stylesheet" href="/css/ash.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="/css/universe.css"><!-- hexo injector head_end start --><link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/Zfour/Butterfly-double-row-display@1.00/cardlistpost.min.css"/>
<style>#recent-posts > .recent-post-item >.recent-post-info > .article-meta-wrap > .tags:before {content:"\A";
  white-space: pre;}#recent-posts > .recent-post-item >.recent-post-info > .article-meta-wrap > .tags > .article-meta__separator{display:none}</style>
<link rel="stylesheet" href="/css/ash.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiperstyle.css" media="print" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/font-awesome-animation.min.css" media="defer" onload="this.media='all'"><link rel="stylesheet" href="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/tag_plugins.css" media="defer" onload="this.media='all'"><script src="https://npm.elemecdn.com/hexo-butterfly-tag-plugins-plus@latest/lib/assets/carousel-touch.js"></script><!-- hexo injector head_end end --><meta name="generator" content="Hexo 7.2.0"></head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><script>(()=>{
  const $loadingBox = document.getElementById('loading-box')
  const $body = document.body
  const preloader = {
    endLoading: () => {
      $body.style.overflow = ''
      $loadingBox.classList.add('loaded')
    },
    initLoading: () => {
      $body.style.overflow = 'hidden'
      $loadingBox.classList.remove('loaded')
    }
  }

  preloader.initLoading()
  window.addEventListener('load',() => { preloader.endLoading() })

  if (true) {
    document.addEventListener('pjax:send', () => { preloader.initLoading() })
    document.addEventListener('pjax:complete', () => { preloader.endLoading() })
  }
})()</script><div id="web_bg"></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="avatar-img is-center"><img src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20240514133138.jpg" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="sidebar-site-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">9</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><hr class="custom-hr"/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tools/"><i class="fa-fw fas fa-tools"></i><span> 工具</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm12.webp')"><nav id="nav"><span id="blog-info"><a href="/" title="Ash's blog"><span class="site-name">Ash's blog</span></a></span><div id="menus"><div id="search-button"><a class="site-page social-icon search" href="javascript:void(0);"><i class="fas fa-search fa-fw"></i><span> 搜索</span></a></div><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 归档</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标签</span></a></div><div class="menus_item"><a class="site-page" href="/categories/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/tools/"><i class="fa-fw fas fa-tools"></i><span> 工具</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-heart"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page" href="javascript:void(0);"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">兴趣课-时钟的实现</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2024-05-21T01:52:57.000Z" title="发表于 2024-05-21 09:52:57">2024-05-21</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2024-05-21T02:19:16.979Z" title="更新于 2024-05-21 10:19:16">2024-05-21</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/HTML/">HTML</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-wordcount"><i class="far fa-file-word fa-fw post-meta-icon"></i><span class="post-meta-label">字数总计:</span><span class="word-count">1.5k</span><span class="post-meta-separator">|</span><i class="far fa-clock fa-fw post-meta-icon"></i><span class="post-meta-label">阅读时长:</span><span>6分钟</span></span><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="兴趣课-时钟的实现"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"><i class="fa-solid fa-spinner fa-spin"></i></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h2 id="一、关于Canvas"><a href="#一、关于Canvas" class="headerlink" title="一、关于Canvas"></a>一、关于Canvas</h2><h4 id="1-什么是canvas"><a href="#1-什么是canvas" class="headerlink" title="1. 什么是canvas"></a>1. 什么是canvas</h4><figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">&lt;canvas&gt;是一个可以使用脚本(通常为JavaScript)在其中绘制图形的 HTML 元素。</span><br></pre></td></tr></table></figure>
<h4 id="2-基础结构"><a href="#2-基础结构" class="headerlink" title="2. 基础结构"></a>2. 基础结构</h4><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line"><span class="tag">&lt;<span class="name">canvas</span> <span class="attr">id</span>=<span class="string">&quot;mycanvas&quot;</span> <span class="attr">width</span>=<span class="string">&quot;200&quot;</span> <span class="attr">height</span>=<span class="string">&quot;300&quot;</span>&gt;</span></span><br><span class="line">   您的浏览器不支持canvas</span><br><span class="line"><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line"></span><br><span class="line"><span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> cc=<span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;mycanvas&quot;</span>);</span></span><br><span class="line"><span class="language-javascript">    <span class="keyword">var</span> cxt=cc.<span class="title function_">getContext</span>(<span class="string">&quot;2d&quot;</span>);</span></span><br><span class="line"><span class="language-javascript"></span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br></pre></td></tr></table></figure>
<p>​    <strong>canvas</strong> 标签只有两个属性—— width和height，这两个属性是可选的。当我们没有定义时，Canvas 的默认大小为300像素×150像素（宽×高，像素的单位是px）。</p>
<p>​    可以像给其他DOM元素一样给canvas添加样式；当开始时没有为canvas规定样式规则，其将会完全透明。</p>
<p>​    canvas是一个二维网格，以左上角坐标为(0,0)</p>
<h4 id="3-绘制矩形"><a href="#3-绘制矩形" class="headerlink" title="3. 绘制矩形"></a>3. 绘制矩形</h4><ul>
<li>参数:<ul>
<li>x与y指定了在canvas画布上所绘制的矩形的左上角（相对于原点）的坐标。</li>
<li>width和height设置矩形的尺寸。</li>
</ul>
</li>
</ul>
<ul>
<li>绘制一个填充的矩形</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">fillRect</span>( x ,y ,width, height)</span><br></pre></td></tr></table></figure>
<ul>
<li>绘制一个矩形的边框</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">strokeRect</span>( x ,y ,width, height)</span><br></pre></td></tr></table></figure>
<h4 id="4-绘制路径"><a href="#4-绘制路径" class="headerlink" title="4. 绘制路径"></a>4. 绘制路径</h4><p>图形的基本元素是路径。路径是通过不同颜色和宽度的线段或曲线相连形成的不同形状的点的集合。一个路径，甚至一个子路径，都是闭合的。使用路径绘制图形需要一些额外的步骤。</p>
<ol>
<li>需要创建路径起始点。</li>
<li>使用画图命令去画出路径。</li>
<li>把路径封闭。</li>
<li>一旦路径生成，就能通过描边或填充路径区域来渲染图形。</li>
</ol>
<p>以下是所要用到的函数：</p>
<ul>
<li>新建一条路径，生成之后，图形绘制命令被指向到路径上生成路径。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">beginPath()</span><br></pre></td></tr></table></figure>
<ul>
<li>闭合路径之后图形绘制命令又重新指向到上下文中。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">closePath()</span><br></pre></td></tr></table></figure>
<p>这个方法会通过绘制一条从当前点到开始点的直线来闭合图形。如果图形是已经闭合了的，即当前点为开始点，该函数什么也不做。</p>
<ul>
<li>通过线条来绘制图形轮廓。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">stroke()</span><br></pre></td></tr></table></figure>
<ul>
<li>通过填充路径的内容区域生成实心的图形。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fill()</span><br></pre></td></tr></table></figure>
<ul>
<li>绘制线段</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">moveTo</span>(x,y)<span class="comment">//移动笔触 </span></span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">lineTo</span>(x,y)<span class="comment">//将笔触移动到指定的坐标x以及y上</span></span><br></pre></td></tr></table></figure>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line">cxt.<span class="title function_">beginPath</span>();</span><br><span class="line">cxt.<span class="title function_">moveTo</span>(<span class="number">150</span>,<span class="number">150</span>);</span><br><span class="line">cxt.<span class="title function_">lineTo</span>(<span class="number">150</span>,<span class="number">250</span>);</span><br><span class="line">cxt.<span class="title function_">lineTo</span>(<span class="number">300</span>,<span class="number">250</span>);</span><br><span class="line">cxt.<span class="title function_">stroke</span>();</span><br><span class="line">cxt.<span class="title function_">closePath</span>();</span><br></pre></td></tr></table></figure>
<h4 id="5-弧线"><a href="#5-弧线" class="headerlink" title="5. 弧线"></a>5. <strong>弧线</strong></h4><p>绘制圆弧或者圆，我们使用arc()方法</p>
<p><img src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/img_arc.gif" alt=""></p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">arc</span>(x, y, radius, startAngle, endAngle, anticlockwise)</span><br></pre></td></tr></table></figure>
<p>画一个以（x,y）为圆心的以radius为半径的圆弧（圆），从startAngle开始到endAngle结束，参数anticlockwise 为一个布尔值。为true时，是逆时针方向，否则顺时针方向。</p>
<p><strong>注意：arc()函数中的角度单位是弧度，不是度数。角度与弧度的js表达式:radians=(Math.PI/180)*degrees。</strong></p>
<p>例子：</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><span class="line">cxt.<span class="title function_">beginPath</span>();</span><br><span class="line">cxt.<span class="title function_">arc</span>(<span class="number">70</span>,<span class="number">70</span>,<span class="number">50</span>,<span class="number">0</span>,<span class="title class_">Math</span>.<span class="property">PI</span>/<span class="number">2</span>,<span class="literal">true</span>);</span><br><span class="line">cxt.<span class="title function_">stroke</span>();</span><br><span class="line"></span><br><span class="line">cxt.<span class="title function_">beginPath</span>();</span><br><span class="line">cxt.<span class="title function_">arc</span>(<span class="number">180</span>,<span class="number">70</span>,<span class="number">50</span>,<span class="number">0</span>,<span class="title class_">Math</span>.<span class="property">PI</span>/<span class="number">2</span>,<span class="literal">false</span>);</span><br><span class="line">cxt.<span class="title function_">stroke</span>();</span><br><span class="line"></span><br><span class="line">cxt.<span class="title function_">beginPath</span>();</span><br><span class="line">cxt.<span class="title function_">arc</span>(<span class="number">300</span>,<span class="number">70</span>,<span class="number">50</span>,<span class="number">0</span>,<span class="title class_">Math</span>.<span class="property">PI</span>/<span class="number">2</span>,<span class="literal">true</span>);</span><br><span class="line">cxt.<span class="title function_">fill</span>();</span><br><span class="line"></span><br><span class="line">cxt.<span class="title function_">beginPath</span>();</span><br><span class="line">cxt.<span class="title function_">arc</span>(<span class="number">400</span>,<span class="number">70</span>,<span class="number">50</span>,<span class="number">0</span>,<span class="title class_">Math</span>.<span class="property">PI</span>/<span class="number">2</span>,<span class="literal">false</span>);</span><br><span class="line">cxt.<span class="title function_">fill</span>();</span><br></pre></td></tr></table></figure>
<h4 id="6-色彩样式"><a href="#6-色彩样式" class="headerlink" title="6. 色彩样式"></a>6. 色彩样式</h4><p>canvas提供了两个色彩属性</p>
<ul>
<li>设置图形的填充颜色。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">fillStyle = color</span><br></pre></td></tr></table></figure>
<ul>
<li>设置图形轮廓的颜色。</li>
</ul>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">strokeStyle = color</span><br></pre></td></tr></table></figure>
<p>注意: 一旦您设置了 strokeStyle 或者 fillStyle 的值，那么这个新值就会成为新绘制的图形的默认值。如果你要给每个图形上不同的颜色，你需要重新设置 fillStyle 或 strokeStyle 的值。</p>
<h4 id="7-线型的样式"><a href="#7-线型的样式" class="headerlink" title="7. 线型的样式"></a>7. <strong>线型的样式</strong></h4><ul>
<li>设置线条宽度：lineWidth = value</li>
</ul>
<h4 id="8-渐变Gradients"><a href="#8-渐变Gradients" class="headerlink" title="8. 渐变Gradients"></a>8. <strong>渐变Gradients</strong></h4><p>我们还可以用线性或者径向的渐变来填充或描边</p>
<ul>
<li>线性渐变</li>
</ul>
<p>createLinearGradient(x1, y1, x2, y2) createLinearGradient 方法接受 4 个参数，表示渐变的起点 (x1,y1) 与终点 (x2,y2)。</p>
<ul>
<li>例子：</li>
</ul>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">var</span> linearGradient=cxt.<span class="title function_">createLinearGradient</span>(<span class="number">50</span>,<span class="number">50</span>,<span class="number">250</span>,<span class="number">250</span>);</span><br><span class="line">  linearGradient.<span class="title function_">addColorStop</span>(<span class="number">0</span>,<span class="string">&#x27;yellow&#x27;</span>);</span><br><span class="line">  linearGradient.<span class="title function_">addColorStop</span>(<span class="number">.5</span>,<span class="string">&#x27;red&#x27;</span>);</span><br><span class="line">  linearGradient.<span class="title function_">addColorStop</span>(<span class="number">1</span>,<span class="string">&#x27;green&#x27;</span>);</span><br><span class="line">  cxt.<span class="property">fillStyle</span>=linearGradient;</span><br><span class="line">  cxt.<span class="title function_">fillRect</span>(<span class="number">50</span>,<span class="number">50</span>,<span class="number">200</span>,<span class="number">200</span>);</span><br></pre></td></tr></table></figure>
<h4 id="9-绘制文本"><a href="#9-绘制文本" class="headerlink" title="9. 绘制文本"></a>9. 绘制文本</h4><p>在指定的(x,y)位置填充指定的文本，绘制的最大宽度是可选的.</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">fillText</span>(text, x, y [, maxWidth])</span><br></pre></td></tr></table></figure>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="title function_">strokeText</span>(text, x, y [, maxWidth])</span><br></pre></td></tr></table></figure>
<h2 id="二、关于JavaScript"><a href="#二、关于JavaScript" class="headerlink" title="二、关于JavaScript"></a>二、关于JavaScript</h2><h4 id="1-setInterval"><a href="#1-setInterval" class="headerlink" title="1. setInterval"></a>1. setInterval</h4><ul>
<li><code>setInterval</code> 是 JavaScript 中的一个定时器函数，用于周期性地执行指定的代码。</li>
</ul>
<p>基本语法结构</p>
<figure class="highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">setInterval(function, milliseconds);</span><br><span class="line"></span><br></pre></td></tr></table></figure>
<ul>
<li><code>function</code>: 要执行的函数或代码块</li>
<li><code>milliseconds</code>: 执行间隔时间（毫秒）</li>
</ul>
<p>使用示例</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><span class="line">javascript复制代码<span class="comment">// 每隔一秒输出一次当前时间</span></span><br><span class="line"><span class="built_in">setInterval</span>(<span class="keyword">function</span>(<span class="params"></span>) &#123;</span><br><span class="line">    <span class="keyword">const</span> date = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line">    <span class="variable language_">console</span>.<span class="title function_">log</span>(date.<span class="title function_">toLocaleTimeString</span>());</span><br><span class="line">&#125;, <span class="number">1000</span>);</span><br></pre></td></tr></table></figure>
<p>注意事项</p>
<ul>
<li><code>setInterval</code> 返回一个唯一的标识符，可通过 <code>clearInterval</code> 来清除定时器。</li>
</ul>
<h4 id="2-Date-对象"><a href="#2-Date-对象" class="headerlink" title="2. Date 对象"></a>2. Date 对象</h4><ul>
<li>JavaScript 中用于处理日期和时间的内置对象。</li>
<li>可以获取当前日期时间，创建特定日期、时间对象。</li>
</ul>
<p>获取当前日期时间</p>
<figure class="highlight js"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">const</span> currentDate = <span class="keyword">new</span> <span class="title class_">Date</span>();</span><br><span class="line"><span class="variable language_">console</span>.<span class="title function_">log</span>(currentDate);</span><br></pre></td></tr></table></figure>
<p>获取小时、分钟、秒、毫秒：<code>getHours()</code>, <code>getMinutes()</code>, <code>getSeconds()</code>,<code>getMilliseconds()</code></p>
<h2 id="三-源码如下"><a href="#三-源码如下" class="headerlink" title="三.源码如下"></a>三.源码如下</h2><figure class="highlight html"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br><span class="line">68</span><br><span class="line">69</span><br><span class="line">70</span><br><span class="line">71</span><br></pre></td><td class="code"><pre><span class="line"><span class="meta">&lt;!DOCTYPE <span class="keyword">html</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">html</span> <span class="attr">lang</span>=<span class="string">&quot;en&quot;</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">head</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">charset</span>=<span class="string">&quot;UTF-8&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">http-equiv</span>=<span class="string">&quot;X-UA-Compatible&quot;</span> <span class="attr">content</span>=<span class="string">&quot;IE=edge&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">meta</span> <span class="attr">name</span>=<span class="string">&quot;viewport&quot;</span> <span class="attr">content</span>=<span class="string">&quot;width=device-width, initial-scale=1.0&quot;</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">title</span>&gt;</span>Document<span class="tag">&lt;/<span class="name">title</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">head</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;<span class="name">body</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">canvas</span> <span class="attr">width</span>=<span class="string">&quot;500&quot;</span> <span class="attr">height</span>=<span class="string">&quot;500&quot;</span> <span class="attr">id</span>=<span class="string">&quot;canvas&quot;</span>&gt;</span><span class="tag">&lt;/<span class="name">canvas</span>&gt;</span></span><br><span class="line">    <span class="tag">&lt;<span class="name">script</span>&gt;</span><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 1.获取画布</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">var</span> canvas = <span class="variable language_">document</span>.<span class="title function_">getElementById</span>(<span class="string">&quot;canvas&quot;</span>)</span></span><br><span class="line"><span class="language-javascript">        <span class="comment">// 2.获取画笔</span></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">var</span> huabi = canvas.<span class="title function_">getContext</span>(<span class="string">&quot;2d&quot;</span>)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        huabi.<span class="property">strokeStyle</span> = <span class="string">&quot;#0ff&quot;</span>;<span class="comment">//画笔颜色</span></span></span><br><span class="line"><span class="language-javascript">        huabi.<span class="property">lineWidth</span> = <span class="number">17</span>;<span class="comment">//画笔宽度</span></span></span><br><span class="line"><span class="language-javascript">        huabi.<span class="property">shadowBlur</span> = <span class="number">15</span><span class="comment">//阴影模糊度 数值越大越模糊</span></span></span><br><span class="line"><span class="language-javascript">        huabi.<span class="property">shadowColor</span> = <span class="string">&quot;#0ff&quot;</span><span class="comment">//阴影颜色</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="keyword">function</span> <span class="title function_">fun</span>(<span class="params"></span>)&#123;</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">//每次重复执行的代码</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 获取时间</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> date = <span class="keyword">new</span> <span class="title class_">Date</span>()</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> today = date.<span class="title function_">toDateString</span>()</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> h = date.<span class="title function_">getHours</span>()</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> m = date.<span class="title function_">getMinutes</span>()</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> s = date.<span class="title function_">getSeconds</span>()</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> ms = date.<span class="title function_">getMilliseconds</span>()</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// console.log( today,h,m,s,ms )</span></span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> s1 = s + (  ms / <span class="number">1000</span> );</span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> m1 = m + ( s1 / <span class="number">60</span> )</span></span><br><span class="line"><span class="language-javascript">            <span class="variable language_">console</span>.<span class="title function_">log</span>( s1 )</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 绘制画布底色</span></span></span><br><span class="line"><span class="language-javascript">            <span class="keyword">var</span> radialGradient =  huabi.<span class="title function_">createRadialGradient</span>(<span class="number">250</span>,<span class="number">250</span>,<span class="number">20</span>,<span class="number">250</span>,<span class="number">250</span>,<span class="number">300</span>)</span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 规定渐变颜色</span></span></span><br><span class="line"><span class="language-javascript">            radialGradient.<span class="title function_">addColorStop</span>(<span class="number">0</span>,<span class="string">&quot;#03303a&quot;</span>)</span></span><br><span class="line"><span class="language-javascript">            radialGradient.<span class="title function_">addColorStop</span>(<span class="number">1</span>,<span class="string">&quot;#000&quot;</span>)</span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="property">fillStyle</span> = radialGradient;</span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">fillRect</span>(<span class="number">0</span>,<span class="number">0</span>,<span class="number">500</span>,<span class="number">500</span>)</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 时</span></span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">beginPath</span>()</span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">arc</span>(<span class="number">250</span>,<span class="number">250</span>,<span class="number">200</span>,<span class="number">270</span> * (<span class="title class_">Math</span>.<span class="property">PI</span> / <span class="number">180</span>),((<span class="number">360</span> / <span class="number">12</span> * h) - <span class="number">90</span>) * (<span class="title class_">Math</span>.<span class="property">PI</span> / <span class="number">180</span>))</span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">stroke</span>()</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 分</span></span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">beginPath</span>()</span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">arc</span>(<span class="number">250</span>,<span class="number">250</span>,<span class="number">170</span>,<span class="number">270</span> * (<span class="title class_">Math</span>.<span class="property">PI</span> / <span class="number">180</span>),((<span class="number">360</span> / <span class="number">60</span> * m1) - <span class="number">90</span>) * (<span class="title class_">Math</span>.<span class="property">PI</span> / <span class="number">180</span>))</span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">stroke</span>()</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            <span class="comment">// 秒</span></span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">beginPath</span>()</span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">arc</span>(<span class="number">250</span>,<span class="number">250</span>,<span class="number">140</span>,<span class="number">270</span> * (<span class="title class_">Math</span>.<span class="property">PI</span> / <span class="number">180</span>),((<span class="number">360</span> / <span class="number">60</span> * s1) - <span class="number">90</span>) * (<span class="title class_">Math</span>.<span class="property">PI</span> / <span class="number">180</span>))</span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">stroke</span>()</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="property">font</span> = <span class="string">&quot;24px 宋体&quot;</span></span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="property">fillStyle</span> = <span class="string">&quot;#0ff&quot;</span></span></span><br><span class="line"><span class="language-javascript">            huabi.<span class="title function_">fillText</span>(today,<span class="number">160</span>,<span class="number">250</span>)</span></span><br><span class="line"><span class="language-javascript">        &#125;</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="title function_">fun</span>()</span></span><br><span class="line"><span class="language-javascript"></span></span><br><span class="line"><span class="language-javascript">        <span class="built_in">setInterval</span>(fun,<span class="number">10</span>)</span></span><br><span class="line"><span class="language-javascript">    </span><span class="tag">&lt;/<span class="name">script</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">body</span>&gt;</span></span><br><span class="line"><span class="tag">&lt;/<span class="name">html</span>&gt;</span></span><br></pre></td></tr></table></figure>
</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta"><i class="fas fa-circle-user fa-fw"></i>文章作者: </span><span class="post-copyright-info"><a href="http://www.ashblog.top">Ash</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta"><i class="fas fa-square-arrow-up-right fa-fw"></i>文章链接: </span><span class="post-copyright-info"><a href="http://www.ashblog.top/2024/05/21/%E5%85%B4%E8%B6%A3%E8%AF%BE-%E6%97%B6%E9%92%9F%E7%9A%84%E5%AE%9E%E7%8E%B0/">http://www.ashblog.top/2024/05/21/%E5%85%B4%E8%B6%A3%E8%AF%BE-%E6%97%B6%E9%92%9F%E7%9A%84%E5%AE%9E%E7%8E%B0/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta"><i class="fas fa-circle-exclamation fa-fw"></i>版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="http://www.ashblog.top" target="_blank">Ash's blog</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/HTML/">HTML</a></div><div class="post_share"><div class="social-share" data-image="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm12.webp" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/sharejs/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2024/05/22/%E5%BB%BA%E7%AB%99%E5%88%9D%E8%A1%B7/" title="建站初衷"><img class="cover" src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm12.webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">建站初衷</div></div></a></div><div class="next-post pull-right"><a href="/2024/05/19/Morkdomn-%E5%BF%AB%E9%80%9F%E5%85%A5%E9%97%A8/" title="Morkdomn 快速入门"><img class="cover" src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/movie.webp" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">Morkdomn 快速入门</div></div></a></div></nav></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="is-center"><div class="avatar-img"><img src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/%E5%BE%AE%E4%BF%A1%E5%9B%BE%E7%89%87_20240514133138.jpg" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/></div><div class="author-info__name">Ash</div><div class="author-info__description">一个渴望成为像陈平安一样的少年</div></div><div class="card-info-data site-data is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">9</div></a><a href="/tags/"><div class="headline">标签</div><div class="length-num">4</div></a><a href="/categories/"><div class="headline">分类</div><div class="length-num">4</div></a></div><a id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/foxlast-code"><i class="fab fa-github"></i><span>🛴前往小家...</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/foxlast-code" target="_blank" title="Github"><i class="fab fa-github" style="color: #24292e;"></i></a><a class="social-icon" href="mailto:m13429938690@163.com" target="_blank" title="Email"><i class="fas fa-envelope" style="color: #4a7dbe;"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn fa-shake"></i><span>公告</span></div><div class="announcement_content">This is my Blog</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span><span class="toc-percentage"></span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%80%E3%80%81%E5%85%B3%E4%BA%8ECanvas"><span class="toc-number">1.</span> <span class="toc-text">一、关于Canvas</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-%E4%BB%80%E4%B9%88%E6%98%AFcanvas"><span class="toc-number">1.0.1.</span> <span class="toc-text">1. 什么是canvas</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-%E5%9F%BA%E7%A1%80%E7%BB%93%E6%9E%84"><span class="toc-number">1.0.2.</span> <span class="toc-text">2. 基础结构</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#3-%E7%BB%98%E5%88%B6%E7%9F%A9%E5%BD%A2"><span class="toc-number">1.0.3.</span> <span class="toc-text">3. 绘制矩形</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#4-%E7%BB%98%E5%88%B6%E8%B7%AF%E5%BE%84"><span class="toc-number">1.0.4.</span> <span class="toc-text">4. 绘制路径</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#5-%E5%BC%A7%E7%BA%BF"><span class="toc-number">1.0.5.</span> <span class="toc-text">5. 弧线</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#6-%E8%89%B2%E5%BD%A9%E6%A0%B7%E5%BC%8F"><span class="toc-number">1.0.6.</span> <span class="toc-text">6. 色彩样式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#7-%E7%BA%BF%E5%9E%8B%E7%9A%84%E6%A0%B7%E5%BC%8F"><span class="toc-number">1.0.7.</span> <span class="toc-text">7. 线型的样式</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#8-%E6%B8%90%E5%8F%98Gradients"><span class="toc-number">1.0.8.</span> <span class="toc-text">8. 渐变Gradients</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#9-%E7%BB%98%E5%88%B6%E6%96%87%E6%9C%AC"><span class="toc-number">1.0.9.</span> <span class="toc-text">9. 绘制文本</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BA%8C%E3%80%81%E5%85%B3%E4%BA%8EJavaScript"><span class="toc-number">2.</span> <span class="toc-text">二、关于JavaScript</span></a><ol class="toc-child"><li class="toc-item toc-level-4"><a class="toc-link" href="#1-setInterval"><span class="toc-number">2.0.1.</span> <span class="toc-text">1. setInterval</span></a></li><li class="toc-item toc-level-4"><a class="toc-link" href="#2-Date-%E5%AF%B9%E8%B1%A1"><span class="toc-number">2.0.2.</span> <span class="toc-text">2. Date 对象</span></a></li></ol></li></ol></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89-%E6%BA%90%E7%A0%81%E5%A6%82%E4%B8%8B"><span class="toc-number">3.</span> <span class="toc-text">三.源码如下</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2024/08/18/%E7%BB%93%E6%9E%84%E4%BD%93/" title="结构体"><img src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/2.jpg" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="结构体"/></a><div class="content"><a class="title" href="/2024/08/18/%E7%BB%93%E6%9E%84%E4%BD%93/" title="结构体">结构体</a><time datetime="2024-08-18T03:44:02.000Z" title="发表于 2024-08-18 11:44:02">2024-08-18</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/06/01/C%E8%AF%AD%E8%A8%80%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84/" title="C语言字符数组"><img src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/music.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C语言字符数组"/></a><div class="content"><a class="title" href="/2024/06/01/C%E8%AF%AD%E8%A8%80%E5%AD%97%E7%AC%A6%E6%95%B0%E7%BB%84/" title="C语言字符数组">C语言字符数组</a><time datetime="2024-06-01T14:06:02.000Z" title="发表于 2024-06-01 22:06:02">2024-06-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/06/01/C%E8%AF%AD%E8%A8%80%E6%95%B0%E7%BB%84/" title="C语言数组"><img src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/movie.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="C语言数组"/></a><div class="content"><a class="title" href="/2024/06/01/C%E8%AF%AD%E8%A8%80%E6%95%B0%E7%BB%84/" title="C语言数组">C语言数组</a><time datetime="2024-06-01T05:47:17.000Z" title="发表于 2024-06-01 13:47:17">2024-06-01</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/05/23/%E6%9E%9A%E4%B8%BE%E7%AE%97%E6%B3%95/" title="枚举算法"><img src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm8.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="枚举算法"/></a><div class="content"><a class="title" href="/2024/05/23/%E6%9E%9A%E4%B8%BE%E7%AE%97%E6%B3%95/" title="枚举算法">枚举算法</a><time datetime="2024-05-23T14:00:02.000Z" title="发表于 2024-05-23 22:00:02">2024-05-23</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2024/05/22/%E5%BB%BA%E7%AB%99%E5%88%9D%E8%A1%B7/" title="建站初衷"><img src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm12.webp" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="建站初衷"/></a><div class="content"><a class="title" href="/2024/05/22/%E5%BB%BA%E7%AB%99%E5%88%9D%E8%A1%B7/" title="建站初衷">建站初衷</a><time datetime="2024-05-22T01:29:22.000Z" title="发表于 2024-05-22 09:29:22">2024-05-22</time></div></div></div></div></div></div></main><footer id="footer"><div id="footer-wrap"><div class="copyright">&copy;2024 By Ash</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside-config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><span class="scroll-percent"></span><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js?v=4.13.0"></script><script src="/js/main.js?v=4.13.0"></script><script src="https://cdn.jsdelivr.net/npm/@fancyapps/ui@5.0.33/dist/fancybox/fancybox.umd.min.js"></script><div class="js-pjax"></div><script src="https://unpkg.com/typed.js@2.1.0/dist/typed.umd.js"></script><canvas id="universe"></canvas><script defer src="/js/universe.js"></script><script defer="defer" id="fluttering_ribbon" mobile="true" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/dist/canvas-fluttering-ribbon.min.js"></script><script src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/dist/activate-power-mode.min.js"></script><script>POWERMODE.colorful = true;
POWERMODE.shake = false;
POWERMODE.mobile = false;
document.body.addEventListener('input', POWERMODE);
</script><script id="click-show-text" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1.1.3/dist/click-show-text.min.js" data-mobile="false" data-text="C语言,是世界上,最美的,语言,富强,民主,文明,和谐,自由,平等,公正,法治,爱国,敬业,诚信,友善" data-fontsize="15px" data-random="false" async="async"></script><script src="https://cdn.jsdelivr.net/npm/pjax@0.2.8/pjax.min.js"></script><script>let pjaxSelectors = ["head > title","#config-diff","#body-wrap","#rightside-config-hide","#rightside-config-show",".js-pjax"]

var pjax = new Pjax({
  elements: 'a:not([target="_blank"]):not([href="/music/"]):not([href="/no-pjax/"])',
  selectors: pjaxSelectors,
  cacheBust: false,
  analytics: false,
  scrollRestoration: false
})

document.addEventListener('pjax:send', function () {

  // removeEventListener
  btf.removeGlobalFnEvent('pjax')
  btf.removeGlobalFnEvent('themeChange')

  document.getElementById('rightside').classList.remove('rightside-show')
  
  if (window.aplayers) {
    for (let i = 0; i < window.aplayers.length; i++) {
      if (!window.aplayers[i].options.fixed) {
        window.aplayers[i].destroy()
      }
    }
  }

  typeof typed === 'object' && typed.destroy()

  //reset readmode
  const $bodyClassList = document.body.classList
  $bodyClassList.contains('read-mode') && $bodyClassList.remove('read-mode')

  typeof disqusjs === 'object' && disqusjs.destroy()
})

document.addEventListener('pjax:complete', function () {
  window.refreshFn()

  document.querySelectorAll('script[data-pjax]').forEach(item => {
    const newScript = document.createElement('script')
    const content = item.text || item.textContent || item.innerHTML || ""
    Array.from(item.attributes).forEach(attr => newScript.setAttribute(attr.name, attr.value))
    newScript.appendChild(document.createTextNode(content))
    item.parentNode.replaceChild(newScript, item)
  })

  GLOBAL_CONFIG.islazyload && window.lazyLoadInstance.update()

  typeof panguInit === 'function' && panguInit()

  // google analytics
  typeof gtag === 'function' && gtag('config', '', {'page_path': window.location.pathname});

  // baidu analytics
  typeof _hmt === 'object' && _hmt.push(['_trackPageview',window.location.pathname]);

  typeof loadMeting === 'function' && document.getElementsByClassName('aplayer').length && loadMeting()

  // prismjs
  typeof Prism === 'object' && Prism.highlightAll()
})

document.addEventListener('pjax:error', e => {
  if (e.request.status === 404) {
    pjax.loadUrl('/404.html')
  }
})</script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script><div id="local-search"><div class="search-dialog"><nav class="search-nav"><span class="search-dialog-title">搜索</span><span id="loading-status"></span><button class="search-close-button"><i class="fas fa-times"></i></button></nav><div class="is-center" id="loading-database"><i class="fas fa-spinner fa-pulse"></i><span>  数据库加载中</span></div><div class="search-wrap"><div id="local-search-input"><div class="local-search-box"><input class="local-search-box--input" placeholder="搜索文章" type="text"/></div></div><hr/><div id="local-search-results"></div><div id="local-search-stats-wrap"></div></div></div><div id="search-mask"></div><script src="/js/search/local-search.js?v=4.13.0"></script></div></div><!-- hexo injector body_end start --> <script data-pjax>if(document.getElementById('recent-posts') && (location.pathname ==='/'|| '/' ==='all')){
    var parent = document.getElementById('recent-posts');
    var child = '<div class="recent-post-item" style="width:100%;height: auto"><div id="catalog_magnet"><div class="magnet_item"><a class="magnet_link" href="http://www.ashblog.top/categories/Butterfly/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">📚 小Aの魔改教程 (1)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="http://www.ashblog.top/categories/蓝桥系列/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🎮 小Aの蓝桥之路 (1)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="http://www.ashblog.top/categories/HTML/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">🐱‍👓 小Aの前端日记 (1)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><div class="magnet_item"><a class="magnet_link" href="http://www.ashblog.top/categories/Study/"><div class="magnet_link_context" style=""><span style="font-weight:500;flex:1">📒 小Aの学习笔记 (1)</span><span style="padding:0px 4px;border-radius: 8px;"><i class="fas fa-arrow-circle-right"></i></span></div></a></div><a class="magnet_link_more"  href="http://www.ashblog.top/categories" style="flex:1;text-align: center;margin-bottom: 10px;">查看更多...</a></div></div>';
    console.log('已挂载magnet')
    parent.insertAdjacentHTML("afterbegin",child)}
     </script><style>#catalog_magnet{flex-wrap: wrap;display: flex;width:100%;justify-content:space-between;padding: 10px 10px 0 10px;align-content: flex-start;}.magnet_item{flex-basis: calc(50% - 5px);background: #f2f2f2;margin-bottom: 10px;border-radius: 8px;transition: all 0.2s ease-in-out;}.magnet_item:hover{background: #69e8f2}.magnet_link_more{color:#555}.magnet_link{color:black}.magnet_link:hover{color:white}@media screen and (max-width: 600px) {.magnet_item {flex-basis: 100%;}}.magnet_link_context{display:flex;padding: 10px;font-size:16px;transition: all 0.2s ease-in-out;}.magnet_link_context:hover{padding: 10px 20px;}</style>
    <style></style><script data-pjax>
  function butterfly_footer_beautify_injector_config(){
    var parent_div_git = document.getElementById('footer-wrap');
    var item_html = '<div id="workboard"></div><p id="ghbdages"><a class="github-badge" target="_blank" href="https://hexo.io/" style="margin-inline:5px" data-title="博客框架为Hexo_v6.2.0" title=""><img src="https://img.shields.io/badge/Frame-Hexo-blue?style=flat&amp;logo=hexo" alt=""/></a><a class="github-badge" target="_blank" href="https://butterfly.js.org/" style="margin-inline:5px" data-title="主题版本Butterfly_v4.3.1" title=""><img src="https://img.shields.io/badge/Theme-Butterfly-6513df?style=flat&amp;logo=bitdefender" alt=""/></a><a class="github-badge" target="_blank" href="https://vercel.com/" style="margin-inline:5px" data-title="本站采用多线部署，主线路托管于Vercel" title=""><img src="https://img.shields.io/badge/Hosted-Vercel-brightgreen?style=flat&amp;logo=Vercel" alt=""/></a><a class="github-badge" target="_blank" href="https://dashboard.4everland.org/" style="margin-inline:5px" data-title="本站采用多线部署，备用线路托管于4EVERLAND" title=""><img src="https://img.shields.io/badge/Hosted-4EVERLAND-22DDDD?style=flat&amp;logo=IPFS" alt=""/></a><a class="github-badge" target="_blank" href="https://github.com/" style="margin-inline:5px" data-title="本站项目由Github托管" title=""><img src="https://img.shields.io/badge/Source-Github-d021d6?style=flat&amp;logo=GitHub" alt=""/></a><a class="github-badge" target="_blank" href="http://creativecommons.org/licenses/by-nc-sa/4.0/" style="margin-inline:5px" data-title="本站采用知识共享署名-非商业性使用-相同方式共享4.0国际许可协议进行许可" title=""><img src="https://img.shields.io/badge/Copyright-BY--NC--SA%204.0-d42328?style=flat&amp;logo=Claris" alt=""/></a></p>';
    console.log('已挂载butterfly_footer_beautify')
    parent_div_git.insertAdjacentHTML("beforeend",item_html)
    }
  var elist = 'null'.split(',');
  var cpage = location.pathname;
  var epage = 'all';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_footer_beautify_injector_config();
  }
  else if (epage === cpage){
    butterfly_footer_beautify_injector_config();
  }
  </script><script async src="/js/runtime.js"></script><script data-pjax>
  function butterfly_swiper_injector_config(){
    var parent_div_git = document.getElementById('recent-posts');
    var item_html = '<div class="recent-post-item" style="height: auto;width: 100%"><div class="blog-slider swiper-container-fade swiper-container-horizontal" id="swiper_container"><div class="blog-slider__wrp swiper-wrapper" style="transition-duration: 0ms;"><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;2024/05/12/hello-world/&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm13.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2024-05-12</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;2024/05/12/hello-world/&quot;);" href="javascript:void(0);" alt="">Hello World</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;2024/05/12/hello-world/&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;2024/05/19/Morkdomn-快速入门/&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/movie.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2024-05-19</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;2024/05/19/Morkdomn-快速入门/&quot;);" href="javascript:void(0);" alt="">Morkdomn 快速入门</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;2024/05/19/Morkdomn-快速入门/&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;2024/05/23/枚举算法/&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm8.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2024-05-23</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;2024/05/23/枚举算法/&quot;);" href="javascript:void(0);" alt="">枚举算法</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;2024/05/23/枚举算法/&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div><div class="blog-slider__item swiper-slide" style="width: 750px; opacity: 1; transform: translate3d(0px, 0px, 0px); transition-duration: 0ms;"><a class="blog-slider__img" onclick="pjax.loadUrl(&quot;2024/05/21/兴趣课-时钟的实现/&quot;);" href="javascript:void(0);" alt=""><img width="48" height="48" src="https://typra-ash.oss-cn-wuhan-lr.aliyuncs.com/dm12.webp" alt="" onerror="this.src=https://unpkg.zhimg.com/akilar-candyassets/image/loading.gif; this.onerror = null;"/></a><div class="blog-slider__content"><span class="blog-slider__code">2024-05-21</span><a class="blog-slider__title" onclick="pjax.loadUrl(&quot;2024/05/21/兴趣课-时钟的实现/&quot;);" href="javascript:void(0);" alt="">兴趣课-时钟的实现</a><div class="blog-slider__text">再怎么看我也不知道怎么描述它的啦！</div><a class="blog-slider__button" onclick="pjax.loadUrl(&quot;2024/05/21/兴趣课-时钟的实现/&quot;);" href="javascript:void(0);" alt="">详情       </a></div></div></div><div class="blog-slider__pagination swiper-pagination-clickable swiper-pagination-bullets"></div></div></div>';
    console.log('已挂载butterfly_swiper')
    parent_div_git.insertAdjacentHTML("afterbegin",item_html)
    }
  var elist = 'undefined'.split(',');
  var cpage = location.pathname;
  var epage = '/';
  var flag = 0;

  for (var i=0;i<elist.length;i++){
    if (cpage.includes(elist[i])){
      flag++;
    }
  }

  if ((epage ==='all')&&(flag == 0)){
    butterfly_swiper_injector_config();
  }
  else if (epage === cpage){
    butterfly_swiper_injector_config();
  }
  </script><script defer src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper.min.js"></script><script defer data-pjax src="https://npm.elemecdn.com/hexo-butterfly-swiper/lib/swiper_init.js"></script><script async src="//at.alicdn.com/t/font_2032782_8d5kxvn09md.js"></script><!-- hexo injector body_end end --></body></html>